package cn.wolfcode.service.impl;

import cn.wolfcode.entity.Userinfo;
import cn.wolfcode.exception.LogicException;
import cn.wolfcode.mapper.UserinfoMapper;
import cn.wolfcode.redis.service.ISecurityRedisService;
import cn.wolfcode.redis.service.IUserInfoRedisService;
import cn.wolfcode.service.IUserinfoService;
import cn.wolfcode.util.AssertUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author c1eye
 * @since 2022-11-03
 */
@Service
public class UserinfoServiceImpl extends ServiceImpl<UserinfoMapper, Userinfo> implements IUserinfoService {

    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @Override
    public boolean checkPhone(String phone) {
        QueryWrapper<Userinfo> wrapper = new QueryWrapper<>();
        wrapper.eq("phone", phone);
        return this.getOne(wrapper) != null;
    }

    @Override
    public void sendVerifyCode(String phone) {
        // 生成
        String code = UUID.randomUUID().toString().replace("-", "").substring(0, 4);

        // 调用京东万象接口发送验证码
//        RestTemplate restTemplate = new RestTemplate();
//        Map<String, Object> params = new HashMap<>();
//        params.put("tos", phone); //访问参数
//        params.put("msg", "【叩丁狼】您的验证码是" + code + " 退订回T"); //访问参数
//        params.put("appkey", "9a1c262db46270f378f2f03351a63dd8");
//        String res = restTemplate.getForObject("https://way.jd.com/jixintong/SMSmarketing?tos={tos}&msg={msg}&appkey={appkey}", String.class, params);
//        if (res == null || !res.contains("Success")) {
//            throw new LogicException("发送失败");
//        }
        System.err.println("yzm: " + code);
        // 发送成功保存验证码到Redis
        userInfoRedisService.setVerifyCode(phone, code);

    }
@Autowired
    ISecurityRedisService securityRedisService;
    @Override
    public void regist(String phone, String nickname, String password, String repassword, String verifyCode, String key) {
        checkParams(phone, nickname, password, repassword, verifyCode);
        // 检查手机号是否注册
        if (checkPhone(phone)) {
            throw new LogicException("手机号已注册");
        }

        String code = userInfoRedisService.getVerifyCode(phone);
        if (code == null) {
            throw new LogicException("请先获取验证码");
        }
        if (!code.equalsIgnoreCase(verifyCode)) {
            throw new LogicException("验证码错误");
        }

        Userinfo userinfo = new Userinfo();
        userinfo.setNickname(nickname);
        userinfo.setPhone(phone);
        userinfo.setPassword(password);
        userinfo.setHeadImgUrl("/images/default.jpg");
        userinfo.setState(0);
        userinfo.setCity("中国");
        userinfo.setInfo("测试用户");
        userinfo.setGender(0);
        userinfo.setEmail("2010782767@qq.com");
        userinfo.setLevel(1);
        save(userinfo);
        securityRedisService.addRegCount(key);
    }

    @Override
    public Userinfo login(String username, String password) {
        QueryWrapper<Userinfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", username).eq("password", password);
        Userinfo userinfo = getOne(queryWrapper);
        if (userinfo == null) {
            throw new LogicException("用户不存在");
        }
        return userinfo;
    }

    @Override
    public List<Userinfo> queryByCity(String name) {
        QueryWrapper<Userinfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("city", name);
        return list(queryWrapper);
    }

    private void checkParams(String phone, String nickname, String password, String repassword, String verifyCode) {
        AssertUtil.hasLength(phone, "电话号为空");
        AssertUtil.hasLength(nickname, "昵称为空");
        AssertUtil.hasLength(password, "密码为空");
        AssertUtil.hasLength(repassword, "确认密码为空");
        AssertUtil.hasLength(verifyCode, "验证码为空");
        AssertUtil.pwdEq(password, repassword, "两次密码不一致");


    }


}
